/*
 * CS_REQ_PURCHASE_PRODUCT_LIST.cpp
 *
 *  Created on: 2013. 5. 28.
 *      Author: the2
 */

#include "../../Command.h"
#include "../../NResource/Static.h"
#include "../../User.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_REQ_PURCHASE_PRODUCT_LIST(User * pUser, ::Network::Packet * pPacket)
{
/*	ZNO_CS_REQ_PURCHASE_PRODUCT_LIST	0x2304
	{
	}

	ZNO_SC_REQ_PURCHASE_PRODUCT_LIST	0x2305
	{
		s16	s16ack;				// 추가 : 2013/07/03
	    u8     u8count;
	    {
			u8	tid;
	        str		strproduct_key;
			u8	u8discount;		// 0 : 할인 없음, 1 : 20%, 2 : 50%
			u32	price;
			u32 amount;			// 젠 : 기본양
			u32 amount_bonus	// 젠 : 보너스양
			u32 amount_rate		// 젠 : 보너스율
	    }[u8count];
	}*/

	PREPARE_PACKET;

//	// 유저에게 현재 젠 상품 이벤트인지 여부 플래그 설정 (트랜잭션 처리 때문에..)
//	pUser->SetZENEventFlag();

	NResource::ProductData * pProduct = NResource::Static::InstancePtr()->GetProductResource()->GetDataPtr(pUser);

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	if (pProduct == NULL)
	{
		DEBUGLOG("CS_REQ_PURCHASE_PRODUCT_LIST : pProduct is NULL!!!!");

		*pAck << static_cast<int16_t>(ACK_UNKNOWN_ERROR);
		*pAck << static_cast<char>(0);
	}
	else
	{
		NResource::ProductData::PRODUCTMAP & clsProductTree = pProduct->GetTree(pUser);
		auto citer = clsProductTree.begin();

		*pAck << static_cast<int16_t>(ACK_OK);
		*pAck << static_cast<char>(clsProductTree.size());

		for ( ; citer != clsProductTree.end(); citer++)
		{
			*pAck << static_cast<char>(citer->second->_TID);
			*pAck << citer->second->_PRODUCT_KEY.c_str();
			*pAck << static_cast<char>(citer->second->_DISCOUNT);
			*pAck << citer->second->_PRICE;
			*pAck << citer->second->_DEFAULT_AMOUNT;
			*pAck << citer->second->_BONUS_AMOUNT;
			*pAck << citer->second->_BONUS_RATE;

			DEBUGLOG("CS_REQ_PURCHASE_PRODUCT_LIST : user_seq=%d, tid=%d, product_key[%s], discount=%d, price=%d",
					pUser->GetUserSeq(), citer->second->_TID, citer->second->_PRODUCT_KEY.c_str(),
					citer->second->_DISCOUNT, citer->second->_PRICE);
		}
	}

	pAck->MakePacket(CMD_SC_REQ_PURCHASE_PRODUCT_LIST);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/

